package com.yeban.test1.utils;

import java.sql.*;

/**
 * @Desc TODO
 * @Author yeban
 * @Date 2025/11/17
 */
public class Test1Utils {

    // 获取连接对象
    public static Connection getConn() {
        try {
            // 1.加载驱动类
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection conn = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/testsql?useUnicode=true&charsetEncoding=UTF8&serverTimezone=Asia/Shanghai&useSSL=false",
                    "root", "123456"
            );
            return conn;
        } catch (ClassNotFoundException | SQLException e) {
            throw new RuntimeException(e);
        }
    }

    // 关闭资源
    public static void close(Statement st, Connection conn, ResultSet rs) {
        if(rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        if(st!=null){
            try {
                st.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
